package com.jzy.bxb.app.scripts.http.order;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jzy.bxb.app.server.AuthHttpHandler;
import com.jzy.bxb.core.handler.HandlerEntity;
import com.jzy.bxb.core.handler.HttpCode;
import com.jzy.bxb.core.handler.HttpHandler;
import com.jzy.bxb.model.constant.OrderState;
import com.jzy.bxb.model.mongo.dao.OrderDao;
import com.jzy.bxb.model.mongo.entity.Order;

/**
 * 查询订单
 * <p>
 * http://127.0.0.1:8680/order/query?type=0&page=1&limit=5&sid=a7c738ac-985d-4685-a725-4e5a9d2f18f3
 * http://112.74.36.233:8680/order/query?type=0&page=1&limit=5&sid=daa0cf5b-e72d-422c-b278-450b28a702c6
 * <br>
 * type 0自己所有下单订单；1帮手所有订单； 2帮手待处理的订单； 3帮手已完成订单 ; 4订单详情 <br>
 * page 页码 <br>
 * limit 每页显示数量<br>
 * orderId 订单ID（type=4）
 * 
 * </p>
 * 
 * @author JiangZhiYong
 * @date 2017-07-11 QQ:359135103
 */
@HandlerEntity(path = "/order/query")
public class QueryOrderHandler extends AuthHttpHandler {

	@Override
	public void run() {

		int type = getInt("type");
		int page = getInt("page");
		int limit = getInt("limit");
		if (page < 1 || limit < 1) {
			sendError("请选择合适的页码");
			return;
		}
		List<Order> list = null;
		long count=0;
		switch (type) {
		case 0:
			list = OrderDao.getEmployerOrders(user.getId(), page, limit);
			count=OrderDao.getEmployerOrderCount(user.getId());
			break;
		case 1:
			list = OrderDao.getAssistantOrders(user.getId(), page, limit);
			count=OrderDao.getAssistantOrderCount(user.getId());
			break;
		case 2:
			list = OrderDao.getAssistantOrders(user.getId(), OrderState.CREATE, page, limit);
			count=OrderDao.getAssistantOrderCount(user.getId(), OrderState.CREATE);
			break;
		case 3:
			list = OrderDao.getAssistantOrders(user.getId(), OrderState.PAY_FINISH, page, limit);
			count=OrderDao.getAssistantOrderCount(user.getId(), OrderState.PAY_FINISH);
			break;
		case 4:
			long orderId = getLong("orderId");
			Order order = OrderDao.getById(orderId);
			if (order == null) {
				sendError("订单不存在");
			} else {
				sendMsg(order);
			}
			return;
		default:
			break;
		}

		if (list != null) {
			Map<String, Object> resMsg = new HashMap<>();
			resMsg.put("code", HttpCode.SUCCESS);
			resMsg.put("count", count);
			resMsg.put("info", JSON.toJSONString(list));
			String resStr = JSON.toJSONString(resMsg);
//			LOGGER.info("{}-->{}", this.getClass().getSimpleName(), resStr);
			getParameter().appendBody(resStr);
			response();
		} else {
			sendError("未查到合适数据");
		}

	}

}
